package alp.starcode.edms.dict.controller;

import alp.starcode.edms.dict.model.dto.DictDataDto;
import alp.starcode.edms.dict.model.vo.DictDataVo;
import alp.starcode.common.mybatis.page.Pagination;
import alp.starcode.common.web.result.Result;
import alp.starcode.edms.dict.service.DictDataService;
import alp.starcode.edms.framework.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 字典数据控制器
 *
 * @author Clxini
 * @since 2025-01-17
 */
@Slf4j
@RestController
@RequestMapping("/dict/data")
@Api(tags = "字典模块-字典数据管理")
@Validated
public class DictDataController extends BaseController {

    @Autowired
    private DictDataService dictDataService;

    @PostMapping("addDictData.do")
    @ApiOperation("新增字典数据")
    public Result<Void> addDictData(@Valid @RequestBody DictDataDto dictDataDto) {
        dictDataService.addDictData(dictDataDto);
        return Result.success();
    }

    @PostMapping("updateDictData.do")
    @ApiOperation("修改字典数据")
    public Result<Void> updateDictData(@Valid @RequestBody DictDataDto dictDataDto) {
        dictDataService.updateDictData(dictDataDto);
        return Result.success();
    }

    @PostMapping("deleteDictData.do")
    @ApiOperation("删除字典数据")
    public Result<Void> deleteDictData(
            @ApiParam(value = "字典数据ID", required = true)
            @RequestParam @NotBlank(message = "字典数据ID不能为空") String dictDataId) {
        
        dictDataService.deleteDictData(dictDataId);
        return Result.success();
    }

    @PostMapping("batchDeleteDictData.do")
    @ApiOperation("批量删除字典数据")
    public Result<Void> batchDeleteDictData(
            @ApiParam(value = "字典数据ID列表", required = true)
            @RequestBody List<String> dictDataIds) {
        
        dictDataService.batchDeleteDictData(dictDataIds);
        return Result.success();
    }

    @GetMapping("getDictDataById.do")
    @ApiOperation("根据ID查询字典数据")
    public Result<DictDataVo> getDictDataById(
            @ApiParam(value = "字典数据ID", required = true)
            @RequestParam @NotBlank(message = "字典数据ID不能为空") String dictDataId) {
        
        return Result.success(dictDataService.getDictDataById(dictDataId));
    }

    @GetMapping("getDictDataListByTypeId.do")
    @ApiOperation("根据字典类型ID查询字典数据列表")
    public Result<List<DictDataVo>> getDictDataListByTypeId(
            @ApiParam(value = "字典类型ID", required = true)
            @RequestParam @NotBlank(message = "字典类型ID不能为空") String dictTypeId) {
        
        return Result.success(dictDataService.getDictDataListByTypeId(dictTypeId));
    }

    @GetMapping("getDictDataListByTypeCode.do")
    @ApiOperation("根据字典类型编码查询字典数据列表")
    public Result<List<DictDataVo>> getDictDataListByTypeCode(
            @ApiParam(value = "字典类型编码", required = true)
            @RequestParam @NotBlank(message = "字典类型编码不能为空") String dictTypeCode) {
        
        return Result.success(dictDataService.getDictDataListByTypeCode(dictTypeCode));
    }

    @GetMapping("pageDictData.do")
    @ApiOperation("分页查询字典数据")
    public Result<Pagination<DictDataVo>> pageDictData(
            @ApiParam(value = "字典类型ID") @RequestParam(required = false) String dictTypeId,
            @ApiParam(value = "字典数据名称") @RequestParam(required = false) String dictDataName,
            @ApiParam(value = "字典数据编码") @RequestParam(required = false) String dictDataCode,
            Pagination<DictDataVo> pagination) {
        
        return Result.success(dictDataService.pageDictData(dictTypeId, dictDataName, dictDataCode, pagination));
    }

    @GetMapping("checkDictDataCodeExists.do")
    @ApiOperation("检查字典数据编码是否存在")
    public Result<Boolean> checkDictDataCodeExists(
            @ApiParam(value = "字典类型ID", required = true) @RequestParam @NotBlank(message = "字典类型ID不能为空") String dictTypeId,
            @ApiParam(value = "字典数据编码", required = true) @RequestParam @NotBlank(message = "字典数据编码不能为空") String dictDataCode,
            @ApiParam(value = "排除的ID") @RequestParam(required = false) String excludeId) {
        
        return Result.success(dictDataService.checkDictDataCodeExists(dictTypeId, dictDataCode, excludeId));
    }
} 